package cn.lzy.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * @Description
 * @Author lzy
 * @Date 2023/12/5 15:16
 * @Version
 **/
@Configuration
//将配置文件注册到 Spring 容器中，作用类似于 <context:property-placeholder location="classpath:db.properties"/>
@PropertySource("classpath:db.properties")
public class JavaConfig {

    @Value("${db.url}")
    private String url;

    @Value("${db.username}")
    private String username;

    @Value("${db.password}")
    private String password;

    @Bean
    DataSource dataSource() {
        DataSource ds = new DataSource();
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        return ds;
    }
}
